package com.yyx.beautifylib.ui.activity;

import android.graphics.Bitmap;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.muzhi.camerasdk.library.filter.GPUImage;
import com.muzhi.camerasdk.library.filter.GPUImageView;
import com.muzhi.camerasdk.library.utils.PhotoEnhance;
import com.yyx.beautifylib.R;
import com.yyx.beautifylib.model.BLSharpenParam;
import com.yyx.beautifylib.model.BLSharpenParam;
import com.yyx.beautifylib.ui.activity.base.BLToolBarActivity;
import com.yyx.beautifylib.utils.MyImageProcessor;

import java.util.Stack;
/**
 * @Desc:锐化
 * @Author:  86133
 * @Time:  2020/4/11
 */
public class BLSharpenActivity extends BLToolBarActivity implements View.OnClickListener {
    private GPUImageView mIvSource;

    private BLSharpenParam mParam;
    private Bitmap mSource;

    private ImageView sharpen_yes;
    private ImageView return_last;

    private Stack<Bitmap> pictureForRevoke;
    private int maxRevokePictures;

    private boolean canChange = true;

    @Override
    protected int getContentLayoutId() {
        return R.layout.bl_activity_sharpen;
    }

    @Override
    protected void customToolBarStyle() {
        mToolbar.setTitle("锐化");
        mToolbar.inflateMenu(R.menu.menu_preview);
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getItemId() == R.id.preview_menu) {
                    setResult(RESULT_OK);
                    BLSharpenParam.recycleBitmap();
                    BLSharpenParam.bitmap = mSource;
                    onBackPressed();
                }
                return false;
            }
        });
    }

    @Override
    protected void initView() {
        mIvSource = getViewById(R.id.enhance_image);

        sharpen_yes = getViewById(R.id.sharpen_yes);
        return_last = getViewById(R.id.return_last);
    }

    @Override
    protected void otherLogic() {
        initRevokeValue();
        mParam = getIntent().getParcelableExtra(BLSharpenParam.KEY);
        mSource = BLSharpenParam.bitmap;

        mIvSource.setScaleType(GPUImage.ScaleType.CENTER_INSIDE);
        mIvSource.setImage(mSource);

    }

    private void initRevokeValue(){
        pictureForRevoke=new Stack<>();
        maxRevokePictures=9;
    }

    private void putPictureIntoRevokeStack(){
        if(pictureForRevoke.size()>=maxRevokePictures){
            pictureForRevoke.remove(0);
        }
        Bitmap currentBitMap = mIvSource.getCurrentBitMap();
        pictureForRevoke.push(currentBitMap);
    }

    @Override
    protected void setListener() {
        sharpen_yes.setOnClickListener(this);
        return_last.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int resId = v.getId();
        if (resId == R.id.sharpen_yes) {
            if(canChange){
                putPictureIntoRevokeStack();
                reliefPicture();
                sharpen_yes.setImageResource(R.drawable.mosaic_three_un);
            }
            canChange = false;
        } else if (resId == R.id.return_last) {
            sharpen_yes.setImageResource(R.drawable.mosaic_three);
            revokePicture();
            canChange = true;
        }
    }

    private void reliefPicture(){
        Bitmap reliefBitmap= MyImageProcessor.sharpenPicture(mSource);
        mSource = reliefBitmap;
        mIvSource.setImage(mSource);
    }

    private void revokePicture(){
        if(!pictureForRevoke.empty()){
            mIvSource.setImage(pictureForRevoke.peek());
            pictureForRevoke.pop();
        }
    }
}
